<?
/*
* 2/4/2008 8:47:24 PM
* Very easy to implement:
* CODE EXAMPLE WITH CONTENTS:

// Expected $_POST 1:
// (
//     [content] => 24 (Hola) // ID (text)
//     [action] => addRelated
// )
// Expected $_POST 2:
// (
//     [related_contentID] => 24
//     [action] => deleteRelated
// )
// Expected $_POST 3:
// (
//     [newOrder] => mySortable[]=24&mySortable[]=52&mySortable[]=22
//     [action] => orderRelated
// )
//
// Setting Content Object:
$indexField = 'contentID';
$content = new contentsContent();
$content->setDBLink($dbLink);
$content->get((int)$_GET['contentID']);


// Setting vars to use the script:
$relatedItem = 'content';
$relatedTable = 'contents_contents';
$relatedPrimaryTableIndex = 'contentID';
$relatedSecondaryTableIndex = 'related_contentID';
$relatedDataObj = $content;
include(DIR_ADMIN "/common/inc.relate_dataobjects.php");

// That's it. $messenger will be created automatically.
*/

$relatedItem = $relatedItem? $relatedItem : 'product';
$relatedTable = $relatedTable? $relatedTable : 'products_products';
$relatedPrimaryTableIndex = $relatedPrimaryTableIndex? $relatedPrimaryTableIndex : 'productID';
$relatedSecondaryTableIndex = $relatedSecondaryTableIndex? $relatedSecondaryTableIndex : 'related_productID';
$relatedDataObj = $relatedDataObj? $relatedDataObj : $product;

if (!$relatedDataObj)
{
    die("Por favor, definir \$relatedDataObj. Gracias.");
}

switch ($_REQUEST['action'])
{
    case 'orderRelated':
        $messenger = new Messenger();
        if ($user->isEnabledTo("write", $module->id))
        {
            $noErrors=false;
            $newOrder = explode('&',$_POST['newOrder']);
            if (is_array($newOrder))
            {
                for($i=0; $i<count($newOrder); $i++)
                {
                    $relatedItemID = intval(str_replace('mySortable[]=', '', $newOrder[$i]));
                    $sql = "UPDATE ". $relatedTable ." SET position='". $i ."' WHERE ". $relatedSecondaryTableIndex ."=". $relatedItemID;
                    $noErrors = mysql_query($sql);
                    if (!$noErrors) break;
                }
                if ($noErrors)
                {
                    $messenger->setMessageType(1);
                    $messenger->addMessage("Lista de items ordenada exitosamente.");
                }
                else $messenger->addMessage("Error al ordenadar la lista de items: ". mysql_error());
            }
        } else $messenger->addMessage($lang->translate("user_action_denied"));
        break;


    case 'addRelated':
        $messenger = new Messenger();
        if ($user->isEnabledTo("write", $module->id))
        {
            // fetching productID
            $relatedItemID = preg_replace("(\([.]*\))", "", $_POST[$relatedItem]);
            $relatedItemID = trim($relatedItemID);
            if ($relatedItemID = strval((int)$relatedItemID))
            {
                $sql = "REPLACE INTO ". $relatedTable ." (". $relatedPrimaryTableIndex .", ". $relatedSecondaryTableIndex .", position)
                        SELECT '". $relatedDataObj->getID() ."', '". $relatedItemID ."', MAX(position)+1 FROM ". $relatedTable ." WHERE ". $relatedPrimaryTableIndex ."=". $relatedDataObj->getID();
                if (mysql_query($sql))
                {
                    $messenger->setMessageType(1);
                    $messenger->addMessage("Item asignado exitosamente.");
                }
                else $messenger->addMessage("Error al agregar el item: ". mysql_error());
            }
            else $messenger->addMessage("ID de item inv&aacute;lido.");
        } else $messenger->addMessage($lang->translate("user_action_denied"));
        break;


    case 'deleteRelated':
        $messenger = new Messenger();
        if ($user->isEnabledTo("write", $module->id))
        {
            $sql = "DELETE FROM ". $relatedTable ." WHERE ". $relatedSecondaryTableIndex ."='". (int)$_GET[$relatedSecondaryTableIndex] ."' AND ". $relatedPrimaryTableIndex ."='". $relatedDataObj->getID() ."'";
            if (mysql_query($sql))
            {
                $messenger->setMessageType(1);
                $messenger->addMessage("Item desasignado exitosamente.");
            }
            else $messenger->addMessage("Error al desasignar el producto: ". mysql_error());
        } else $messenger->addMessage($lang->translate("user_action_denied"));
        break;
}

?>
